<template>
  <div>
    <div class="ball-container">
      <div v-for="(item, index) in balls" :key="index">
        <transition
          @before-enter="beforeEnter(item, $event)"
          @enter="enter(item, $event)"
          @after-enter="afterEnter(item, $event)"
        >
          <div class="boll" v-if="item.show">
            <div class="inner inner-hook"></div>
          </div>
        </transition>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'

const balls = ref([{ show: false }, { show: false }, { show: false }, { show: false }, { show: false }])

const dropBalls = ref([])

const drop = (el) => {
  for (let i = 0; i < balls.value.length; i++) {
    const ball = balls.value[i]
    if (!ball.show) {
      ball.show = true
      ball.el = el
      dropBalls.value.push(ball)
      return
    }
  }
}

const beforeEnter = (item, el) => {
  const rect = item.el.getBoundingClientRect()
  const x = rect.left - 32
  const y = -(window.innerHeight - rect.top - 22)
  el.style.display = 'block'
  el.style.transform = `translate3d(0,${y}px,0)`
  const inner = el.getElementsByClassName('inner-hook')[0]
  inner.style.transform = `translate3d(${x}px,0,0)`
}

const enter = (item, el) => {
  item._reflow = document.body.offsetHeight || document.documentElement.offsetHeight
  el.style.transform = `translate3d(0,0,0)`
  const inner = el.getElementsByClassName('inner-hook')[0]
  inner.style.transform = `translate3d(0,0,0)`
}

const afterEnter = (item, el) => {
  const ball = dropBalls.value.shift()
  if (ball) {
    ball.show = false
    el.style.display = 'none'
  }
}
</script>

<style scoped>
.boll {
  /* 外层 */
  position: fixed;
  bottom: 22px;
  left: 32px;
  transition: all 0.4s cubic-bezier(0.49, -0.29, 0.75, 0.41);
}

/* 里层 */
.inner {
  width: 16px;
  height: 15px;
  border-radius: 50%;
  background: blue;
  transition: all 0.4s linear;
}
</style>
